using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class NewBootUo_Manager : MonoBehaviour
{
	[SerializeField]
	private int MessengerCutsceneIndex;

	[SerializeField]
	private float CutsceneMultiplier = 1f;

	[Space(10f)]
	[Header("-= References =-")]
	[SerializeField]
	private Transform Camera;

	[SerializeField]
	private TRIG_LEVELTRANSITION LevelTransition;

	[SerializeField]
	private AudioSource source;

	[SerializeField]
	private AudioClip snd_jingle;

	[SerializeField]
	private AudioClip[] snd_typesounds;

	[SerializeField]
	private AudioClip snd_startup;

	[SerializeField]
	private AudioClip snd_notification;

	[SerializeField]
	private AudioClip snd_chatshow;

	[SerializeField]
	private NewBootUp_NewScreen NewBootUpScreen;

	[SerializeField]
	private Animator HypoasterArmsAnimator;

	[Space(5f)]
	[Header("Computer Screen")]
	[SerializeField]
	private SpriteRenderer ScreenButtonLight;

	[SerializeField]
	private GameObject ScreenWhite;

	[SerializeField]
	private GameObject HourGlass;

	[Space(5f)]
	[Header("BIOS loading Text")]
	[SerializeField]
	private TextMeshProUGUI BIOSTextMesh;

	[TextArea]
	[SerializeField]
	private string BIOSText;

	[SerializeField]
	private float BIOSTextCharPerFrame = 2f;

	[Space(5f)]
	[Header("Logo Glitched")]
	[SerializeField]
	private Image LogoGlitched;

	[Space(5f)]
	[Header("Messenger")]
	[SerializeField]
	private GameObject Messenger;

	[SerializeField]
	private TMP_InputField MessengerInputField;

	[SerializeField]
	private TextMeshProUGUI MessageWindow;

	[SerializeField]
	private bool ForceFocusOnText;

	[SerializeField]
	private string previousAnswer;

	[SerializeField]
	private List<string> CurrentHostMessageQueue = new List<string>();

	[SerializeField]
	private List<bool> CurrentHostMessageFinale = new List<bool>();

	[SerializeField]
	private List<float> CurrentHostMessageLengths = new List<float>();

	private void Start()
	{
		if (PlayerPrefs.GetInt("Game_SingularVesselEditorMode", 0) == 1)
		{
			NewBootUpScreen.StartCoroutine(NewBootUpScreen.VesselCreator());
		}
		else
		{
			StartCoroutine(BOOTUP_Cutscene());
		}
	}

	private IEnumerator BOOTUP_Cutscene()
	{
		yield return new WaitForSeconds(0.5f);
		yield return new WaitForSeconds(1f / CutsceneMultiplier);
		ScreenButtonLight.color = new Color32(byte.MaxValue, 108, 0, byte.MaxValue);
		source.PlayOneShot(snd_startup, 0.5f);
		source.PlayDelayed(0.5f);
		ScreenButtonLight.enabled = true;
		yield return new WaitForSeconds(1f / CutsceneMultiplier);
		ScreenWhite.SetActive(value: true);
		yield return new WaitForSeconds(1f / CutsceneMultiplier);
		ScreenWhite.SetActive(value: false);
		yield return new WaitForSeconds(0.35f / CutsceneMultiplier);
		ScreenButtonLight.color = new Color32(0, byte.MaxValue, 0, byte.MaxValue);
		yield return new WaitForSeconds(1.5f / CutsceneMultiplier);
		int TargetVisibleCharacterCount = BIOSText.Length;
		BIOSTextMesh.maxVisibleCharacters = 0;
		BIOSTextMesh.text = BIOSText;
		StartCoroutine(BIOSSpeedRandomizer());
		while (BIOSTextMesh.maxVisibleCharacters < TargetVisibleCharacterCount)
		{
			yield return new WaitForSeconds(0f);
			BIOSTextMesh.maxVisibleCharacters += (int)(BIOSTextCharPerFrame * CutsceneMultiplier * Time.timeScale);
			if (BIOSTextMesh.maxVisibleCharacters < BIOSTextMesh.text.Length && BIOSTextMesh.text.ToCharArray()[BIOSTextMesh.maxVisibleCharacters] == '|')
			{
				yield return new WaitForSeconds(0.15f);
			}
		}
		yield return new WaitForSeconds(0.25f / CutsceneMultiplier);
		BIOSTextMesh.enabled = false;
		yield return new WaitForSeconds(0.5f / CutsceneMultiplier);
		HourGlass.SetActive(value: true);
		yield return new WaitForSeconds(0.5f / CutsceneMultiplier);
		HourGlass.SetActive(value: false);
		yield return new WaitForSeconds(0.25f / CutsceneMultiplier);
		LogoGlitched.fillAmount = 0.127f;
		yield return new WaitForSeconds(0.1f / CutsceneMultiplier);
		LogoGlitched.fillAmount = 0.256f;
		yield return new WaitForSeconds(0.32f / CutsceneMultiplier);
		LogoGlitched.fillAmount = 0.3f;
		yield return new WaitForSeconds(0.25f / CutsceneMultiplier);
		LogoGlitched.fillAmount = 0.73f;
		yield return new WaitForSeconds(0.5f / CutsceneMultiplier);
		LogoGlitched.fillAmount = 1f;
		yield return new WaitForSeconds(0.1f / CutsceneMultiplier);
		source.PlayOneShot(snd_jingle);
		yield return new WaitForSeconds(3f);
		LogoGlitched.enabled = false;
		yield return new WaitForSeconds(0.1f);
		NewBootUpScreen.StartCoroutine(NewBootUpScreen.Cutscene());
	}

	public IEnumerator DisclaimerCutscene()
	{
		yield return new WaitForSeconds(1f);
		Messenger.SetActive(value: true);
		source.PlayOneShot(snd_chatshow, 0.85f);
		ForceFocusOnText = false;
		yield return new WaitForSeconds(1.5f);
		AddNewHostMessage("Greetings!", EnableOnEnd: false);
		yield return new WaitForSeconds(2f);
		AddNewHostMessage("Are you receiving these messages?", EnableOnEnd: true);
		StartCoroutine(PostHostLoop());
	}

	public void PlayTypeSFX()
	{
		source.PlayOneShot(snd_typesounds[Random.Range(0, snd_typesounds.Length)], 0.25f);
	}

	private void RunMessengerCutscene()
	{
		switch (MessengerCutsceneIndex)
		{
		case 1:
			if (PreviousMessageHadValidAnswer())
			{
				AddNewHostMessage("Excellent!", EnableOnEnd: false, 2f);
				AddNewHostMessage("We've had issues with new connections in the past, you see.", EnableOnEnd: false, 3f);
				AddNewHostMessage("It's not often we recruit new members.", EnableOnEnd: false);
			}
			else
			{
				AddNewHostMessage("Haha.", EnableOnEnd: false, 2f);
				AddNewHostMessage("Very funny.", EnableOnEnd: false, 2f);
				AddNewHostMessage("But in the future, please refrain from illogical responses.", EnableOnEnd: false, 3f);
			}
			AddNewHostMessage("", EnableOnEnd: false, 0.25f);
			AddNewHostMessage("I'll begin with an introduction.", EnableOnEnd: false);
			AddNewHostMessage("I am one of the <color=green>NETWORK ADMINS</color>.", EnableOnEnd: false, 3f);
			AddNewHostMessage("We are only one below the highest ranking in the <color=green>NETWORK</color>.", EnableOnEnd: false, 3f);
			AddNewHostMessage("You will report to us when requested, and keep us updated on your divisions research.", EnableOnEnd: false, 4f);
			AddNewHostMessage("", EnableOnEnd: false, 0.5f);
			AddNewHostMessage("Do you understand?", EnableOnEnd: true);
			break;
		case 2:
			if (!PreviousMessageHadValidAnswer())
			{
				AddNewHostMessage("Understood, Let me explain in layman's terms.", EnableOnEnd: false);
				AddNewHostMessage("<color=green>NETWORK ADMINS</color>, such as myself, are your boss.", EnableOnEnd: false, 3f);
				AddNewHostMessage("You listen to what we say, you do as we say, you feel as we say.", EnableOnEnd: false, 3f);
				AddNewHostMessage("", EnableOnEnd: false, 0.25f);
			}
			AddNewHostMessage("Moving on.", EnableOnEnd: false);
			AddNewHostMessage("In the orientation sequence before, you successfully setup your profile.", EnableOnEnd: false, 3f);
			AddNewHostMessage("Though, just incase something went wrong, I will ask the following.", EnableOnEnd: false, 3f);
			AddNewHostMessage("Are you user, \"DR. HYPOASTER\" with an avatar representation labelled, \"" + PlayerPrefs.GetString("Game_PlayerName", "YOU") + "\"?", EnableOnEnd: true, 3f);
			break;
		case 3:
			if (PreviousMessageHadValidAnswer())
			{
				AddNewHostMessage("Perfect!", EnableOnEnd: false, 2f);
			}
			else
			{
				AddNewHostMessage("Noted.", EnableOnEnd: false, 2f);
			}
			AddNewHostMessage("Once this meeting is over, you may adjust those settings on the home screen.", EnableOnEnd: false, 4f);
			AddNewHostMessage("Now onto the agreements.", EnableOnEnd: false);
			AddNewHostMessage("", EnableOnEnd: false, 0.25f);
			AddNewHostMessage("Firstly, do you understand the <color=green>SIMULATION</color> is a seperate world, detatched from anything currently existing?", EnableOnEnd: true, 4f);
			break;
		case 4:
			if (!PreviousMessageHadValidAnswer())
			{
				AddNewHostMessage("Allow me to educate you.", EnableOnEnd: false, 2.5f);
				AddNewHostMessage("The <color=green>SIMULATION</color>, the <color=green>NETWORK</color> and your own existence.", EnableOnEnd: false);
				AddNewHostMessage("It's all independent from whatever you may remember from your past.", EnableOnEnd: false, 3f);
			}
			AddNewHostMessage("", EnableOnEnd: false, 0.25f);
			AddNewHostMessage("Next, are you content with possible irregularities and inaccuracies within the <color=green>SIMULATION</color> itself?", EnableOnEnd: true);
			break;
		case 5:
			if (!PreviousMessageHadValidAnswer())
			{
				AddNewHostMessage("This will be a problem.", EnableOnEnd: false, 2.5f);
				AddNewHostMessage("The <color=green>SIMULATION</color> is prone to these faults, as is the nature of science itself.", EnableOnEnd: false);
				AddNewHostMessage("I trust that as a scientist yourself, you'll grow to disregard any anomalies.", EnableOnEnd: false, 3f);
				AddNewHostMessage("", EnableOnEnd: false, 0.25f);
			}
			AddNewHostMessage("If any are of true issue, do not slam your desk in anger.", EnableOnEnd: false, 4f);
			AddNewHostMessage("Instead, contact the head of the <color=green>SIMULATION RESEARCH DIVISION</color> or the <color=purple>Manufacturer.</color>", EnableOnEnd: false, 5f);
			AddNewHostMessage("And finally, are you prone to pain and or seizure?", EnableOnEnd: false, 3f);
			AddNewHostMessage("Do feel free to pinch yourself to test this.", EnableOnEnd: true, 3f);
			break;
		case 6:
			AddNewHostMessage("Noted.", EnableOnEnd: false);
			AddNewHostMessage("You can lessen visual strain in the settings menu by toggling \"SIMPLE VFX\"", EnableOnEnd: false, 5f);
			AddNewHostMessage("There are also many other useful settings for you to tinker with.", EnableOnEnd: false, 4f);
			AddNewHostMessage("", EnableOnEnd: false, 0.25f);
			AddNewHostMessage("And with that, Dr. Hypoaster, we have reached the end of this meeting.", EnableOnEnd: false, 4f);
			AddNewHostMessage("I truly thank you for your time, and your cooperation.", EnableOnEnd: false);
			AddNewHostMessage("If you have any questions, do not hesitate to reach out.", EnableOnEnd: false);
			AddNewHostMessage("I have no doubt you will be a particularly large help to our cause.", EnableOnEnd: false, 4f);
			AddNewHostMessage("I would be lying if I didn't say I'm quite excited to see what you bring to the table.", EnableOnEnd: false, 5f);
			AddNewHostMessage("Anywho, goodbye for now \nDr. Hypoaster.", EnableOnEnd: false, 3f);
			AddNewHostMessage("The head of the <color=green>SIMULATION RESEARCH DIVISION</color> will reach out to you shortly.", EnableOnEnd: false, 5f);
			AddNewHostMessage("And in due time.", EnableOnEnd: false, 2.5f);
			AddNewHostMessage("<i>We shall actualize our dreams.</i>", EnableOnEnd: false, 4f);
			AddNewHostMessage("\"", EnableOnEnd: false);
			break;
		case 0:
			break;
		}
	}

	private bool PreviousMessageHadValidAnswer()
	{
		char[] array = previousAnswer.ToUpper().ToCharArray();
		string input = previousAnswer.ToUpper();
		if (array.Length == 0)
		{
			return false;
		}
		string pattern = "(YES|YEP|YEA(?:H)?|YUP|OKAY|ACKNOWLEDGED|UNDERSTAND|AGREE2ALL|MHM|OK|SI|SÍ|是(?:的|滴)?|对|好(?:的|滴)?|可以|嗯|当然|没问题|能接受|接受|可以|能|当然|确认|收到|了解|明白|晓得|认可|赞同|同意|支持|能|理解|清楚|懂|可|行|成|没毛病|没错|连(?:接)?上了|已阅|知道|知晓)";
		string pattern2 = "(否|不是(?:的|滴)?|不对|不可以|不赞同|没有|没|不同意|不接受|不能接受|不可以|不行|不能|不理解|不懂|没连上|未连接|不清楚|不了解|不支持|不认可|没明白|不明白|不知道|不知晓)";
		if (!Regex.IsMatch(input, pattern2, RegexOptions.IgnoreCase))
		{
			return Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase);
		}
		return false;
	}

	private void Update()
	{
		if (ForceFocusOnText)
		{
			EventSystem.current.SetSelectedGameObject(MessengerInputField.gameObject, null);
			MessengerInputField.gameObject.SetActive(value: true);
			MessengerInputField.OnPointerClick(new PointerEventData(EventSystem.current));
			HypoasterArmsAnimator.SetBool("HoldingBackspace", Input.GetKey(KeyCode.Backspace));
			if (Input.anyKeyDown && !Input.GetMouseButtonDown(0) && !Input.GetMouseButtonDown(1) && !Input.GetMouseButtonDown(2) && !Input.GetMouseButtonDown(3) && !Input.GetKey(KeyCode.Backspace))
			{
				HypoasterArmsAnimator.Play($"NewBootUp_HypoasterArms_Type{Random.Range(1, 5)}", -1, 0f);
				CutsceneUtils.PlaySound(snd_typesounds[Random.Range(0, snd_typesounds.Length)], CutsceneUtils.DRH_MixerChannels.Effect, Random.Range(0.65f, 0.85f), Random.Range(0.9f, 1.1f));
			}
			if (Input.GetKeyDown(KeyCode.Backspace))
			{
				CutsceneUtils.PlaySound(snd_typesounds[Random.Range(0, snd_typesounds.Length)], CutsceneUtils.DRH_MixerChannels.Effect, Random.Range(0.65f, 0.85f), Random.Range(0.9f, 1.1f));
			}
			if (Input.GetKeyDown(KeyCode.Return) && MessengerInputField.text.Length > 0)
			{
				MessengerCutsceneIndex++;
				AddNewClientMessage(MessengerInputField.text.ToUpper());
				previousAnswer = MessengerInputField.text;
				StartCoroutine(RunMessageCutsceneDelay());
				MessengerInputField.gameObject.SetActive(value: false);
				MessengerInputField.text = "";
				HypoasterArmsAnimator.Play("NewBootUp_HypoasterArms_StopTyping");
				ForceFocusOnText = false;
			}
		}
		else
		{
			EventSystem.current.SetSelectedGameObject(null, null);
			MessengerInputField.gameObject.SetActive(value: false);
			MessengerInputField.text = "";
		}
	}

	private IEnumerator RunMessageCutsceneDelay()
	{
		yield return new WaitForSeconds(1.5f);
		RunMessengerCutscene();
	}

	private IEnumerator PostHostLoop()
	{
		yield return new WaitForSeconds(0.1f);
		if (CurrentHostMessageQueue.Count > 0)
		{
			yield return new WaitForSeconds(CurrentHostMessageLengths[0]);
			if ((double)CurrentHostMessageLengths[0] >= 0.25)
			{
				PlayHostMessage(CurrentHostMessageQueue[0]);
			}
			else
			{
				PlayHostMessage(CurrentHostMessageQueue[0], PlaySound: false);
			}
			if (CurrentHostMessageQueue[0] == "\"")
			{
				StartCoroutine(DelayUntilLevelTransition());
			}
			CurrentHostMessageQueue.RemoveAt(0);
			CurrentHostMessageLengths.RemoveAt(0);
			if (CurrentHostMessageFinale[0])
			{
				ForceFocusOnText = true;
				HypoasterArmsAnimator.Play("NewBootUp_HypoasterArms_PrepareTyping");
			}
			CurrentHostMessageFinale.RemoveAt(0);
		}
		StartCoroutine(PostHostLoop());
	}

	private void PlayHostMessage(string message, bool PlaySound = true)
	{
		if (message != "")
		{
			if (message != "\"")
			{
				if (PlaySound)
				{
					source.PlayOneShot(snd_notification, 1.25f);
				}
				TextMeshProUGUI messageWindow = MessageWindow;
				messageWindow.text = messageWindow.text + "<5Zi0> " + message + "\n\n";
			}
		}
		else
		{
			TextMeshProUGUI messageWindow2 = MessageWindow;
			messageWindow2.text = messageWindow2.text + message + "\n\n";
		}
	}

	private void AddNewHostMessage(string message, bool EnableOnEnd, float Length = 3.5f)
	{
		CurrentHostMessageQueue.Add(message ?? "");
		CurrentHostMessageFinale.Add(EnableOnEnd);
		CurrentHostMessageLengths.Add(Length);
	}

	private IEnumerator DelayUntilLevelTransition()
	{
		yield return new WaitForSeconds(1f);
		PlayerPrefs.SetInt("NewDisclaimerViewed", 1);
		LevelTransition.BeginTransition(0.35f);
	}

	private void AddNewClientMessage(string message)
	{
		TextMeshProUGUI messageWindow = MessageWindow;
		messageWindow.text = messageWindow.text + "<HYPOASTER> " + message + "\n\n";
	}

	private IEnumerator BIOSSpeedRandomizer()
	{
		if (BIOSTextMesh.maxVisibleCharacters < BIOSText.Length)
		{
			yield return new WaitForSeconds(Random.Range(0.25f, 2f) / CutsceneMultiplier);
			BIOSTextCharPerFrame = Random.Range(3, 6);
			StartCoroutine(BIOSSpeedRandomizer());
		}
	}
}
